package com.xforceplus.bsstool.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xforceplus.bsstool.db.NewUcenterDB;
import com.xforceplus.bsstool.db.OldUcenterDB;
import com.xforceplus.bsstool.log.LogMain;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceStep7 {

  /**
   * 获取老运营角色列表
   * @return
   */
  public static List<Map> getOldRoleList(Long tenantId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_role t where t.group_id = ").append(tenantId);
    logger.info("执行老运营角色获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营角色列表
   * @return
   */
  public static Map<String,Map> getNewRoleMap(Long tenantId,LogMain logger) throws Exception {
    Map<String,Map> newRoleMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_role t where t.tenant_id = ").append(tenantId);
    logger.info("执行新运营角色获取，执行SQL: "+sql.toString());
    List<Map> newRoleList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newRoleMap : newRoleList){
      newRoleMapMap.put(String.valueOf(newRoleMap.get("role_id")),newRoleMap);
    }
    return newRoleMapMap;
  }

  /**
   * 比对新运营角色列表
   * @return
   */
  public static boolean compareRoleWithNew(List<Map> oldRoleList,Long tenantId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newRoleMapMap  = getNewRoleMap(tenantId,logger);
    for (Map oldRoleMap : oldRoleList){
      System.out.println("开始执行角色比对 角色ID:"+oldRoleMap.get("role_id"));
      logger.info("开始执行角色比对 角色ID:"+oldRoleMap.get("role_id"));
      //查询新运营对应功能集
      Map newRoleMap =newRoleMapMap.get(String.valueOf(oldRoleMap.get("role_id")));
      if (newRoleMap == null){
        logger.warn("角色ID:"+oldRoleMap.get("role_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同角色ID,继续执行数据信息比对 新运营角色："+ JSON.toJSONString(newRoleMap));
        //新老数据比对： tenant_id role_code  role_name  status
        oldRoleMap.put("tenant_id",oldRoleMap.get("group_id"));
        result = ServiceCommon.compareMapValue(oldRoleMap,newRoleMap,
                Lists.newArrayList("tenant_id","role_code","role_name","status"),
                "角色ID:"+oldRoleMap.get("role_id"),
                logger);
      }

      //重复角色检查
      List<Map> sameRoleList = NewUcenterDB.getSqlResult("select * from sys_role where role_id != "+oldRoleMap.get("role_id")
              +" and tenant_id = "+oldRoleMap.get("group_id")
              +" and status = "+oldRoleMap.get("status")
              +" and role_code = '"+oldRoleMap.get("role_code")+"'");
      if (!CollectionUtils.isEmpty(sameRoleList)){
        logger.error("老运营角色ID:"+oldRoleMap.get("role_id")+" 新运营存在重复角色记录个数："+sameRoleList.size());
        result = false;
      }
    }
    return result;
  }


  /**
   * 获取老运营角色功能集关系列表
   * @return
   */
  public static List<Map> getOldRoleResourceSetRelList(Long tenantId,Long roleId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_role_resourceset_rel t where t.group_id = ")
            .append(tenantId).append(" and role_id =").append(roleId);
    logger.info("执行老运营角色功能集关系获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营角色功能集关系列表
   * @return
   */
  public static Map<String,Map> getNewRoleResourceSetRelMap(Long tenantId,Long roleId,LogMain logger) throws Exception {
    Map<String,Map> newRoleResourceSetRelMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_role_resourceset_rel t where t.tenant_id = ")
            .append(tenantId).append(" and role_id =").append(roleId);
    logger.info("执行新运营角色功能集关系获取，执行SQL: "+sql.toString());
    List<Map> newRoleResourceSetRelList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newRoleResourceSetRelMap : newRoleResourceSetRelList){
      newRoleResourceSetRelMapMap.put(String.valueOf(newRoleResourceSetRelMap.get("id")),newRoleResourceSetRelMap);
    }
    return newRoleResourceSetRelMapMap;
  }

  /**
   * 比对新运营角色功能集关系列表
   * @return
   */
  public static boolean compareRoleResourceSetRelWithNew(List<Map> oldRoleResourceSetRelList,Long tenantId,Long roleId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newRoleResourceSetRelMapMap  = getNewRoleResourceSetRelMap(tenantId,roleId,logger);
    for (Map oldRoleResourceSetRelMap : oldRoleResourceSetRelList){
      System.out.println("开始执行角色功能集关系比对 角色功能集关系ID:"+oldRoleResourceSetRelMap.get("id"));
      logger.info("开始执行角色功能集关系比对 角色功能集关系ID:"+oldRoleResourceSetRelMap.get("id"));
      //查询新运营对应功能集
      Map newRoleResourceSetRelMap =newRoleResourceSetRelMapMap.get(String.valueOf(oldRoleResourceSetRelMap.get("id")));
      if (newRoleResourceSetRelMap == null){
        logger.warn("角色功能集关系ID:"+oldRoleResourceSetRelMap.get("id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同角色功能集关系ID,继续执行数据信息比对 新运营角色功能集关系："+ JSON.toJSONString(newRoleResourceSetRelMap));
        //新老数据比对： tenant_id role_id resouseset_id
        oldRoleResourceSetRelMap.put("tenant_id",oldRoleResourceSetRelMap.get("group_id"));
        result = ServiceCommon.compareMapValue(oldRoleResourceSetRelMap,newRoleResourceSetRelMap,
                Lists.newArrayList("tenant_id","role_id","resouseset_id"),
                "角色功能集关系ID:"+oldRoleResourceSetRelMap.get("id"),
                logger);
      }

      //重复角色功能集关系检查
      List<Map> sameRoleResourceSetRelList = NewUcenterDB.getSqlResult("select * from sys_role_resourceset_rel where id != "+oldRoleResourceSetRelMap.get("id")
              +" and tenant_id = "+oldRoleResourceSetRelMap.get("group_id")
              +" and resouseset_id = "+oldRoleResourceSetRelMap.get("resouseset_id")
              +" and role_id = "+oldRoleResourceSetRelMap.get("role_id"));
      if (!CollectionUtils.isEmpty(sameRoleResourceSetRelList)){
        logger.error("老运营角色功能集关系ID:"+oldRoleResourceSetRelMap.get("id")+" 新运营存在重复角色功能集关系记录个数："+sameRoleResourceSetRelList.size());
        result = false;
      }
    }
    return result;
  }



  /**
   * 获取老运营角色人员关系列表
   * @return
   */
  public static List<Map> getOldRoleUserRelList(Long tenantId,Long roleId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_role_user_rel t where t.group_id = ")
            .append(tenantId).append(" and role_id =").append(roleId);
    logger.info("执行老运营角色人员关系获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营角色人员关系列表
   * @return
   */
  public static Map<String,Map> getNewRoleUserRelMap(Long tenantId,Long roleId,LogMain logger) throws Exception {
    Map<String,Map> newRoleUserRelMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_role_user_rel t where t.tenant_id = ")
            .append(tenantId).append(" and role_id =").append(roleId);
    logger.info("执行新运营角色人员关系获取，执行SQL: "+sql.toString());
    List<Map> newRoleUserRelList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newRoleUserRelMap : newRoleUserRelList){
      newRoleUserRelMapMap.put(String.valueOf(newRoleUserRelMap.get("id")),newRoleUserRelMap);
    }
    return newRoleUserRelMapMap;
  }

  /**
   * 比对新运营角色人员关系列表
   * @return
   */
  public static boolean compareRoleUserRelWithNew(List<Map> oldRoleUserRelList,Long tenantId,Long roleId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newRoleUserRelMapMap  = getNewRoleUserRelMap(tenantId,roleId,logger);
    for (Map oldRoleUserRelMap : oldRoleUserRelList){
      System.out.println("开始执行角色人员关系比对 角色人员关系ID:"+oldRoleUserRelMap.get("id"));
      logger.info("开始执行角色人员关系比对 角色人员关系ID:"+oldRoleUserRelMap.get("id"));
      //查询新运营对应功能集
      Map newRoleUserRelMap =newRoleUserRelMapMap.get(String.valueOf(oldRoleUserRelMap.get("id")));
      if (newRoleUserRelMap == null){
        logger.warn("角色人员关系ID:"+oldRoleUserRelMap.get("id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同角色人员关系ID,继续执行数据信息比对 新运营角色人员关系："+ JSON.toJSONString(newRoleUserRelMap));
        //新老数据比对： tenant_id role_id user_id
        oldRoleUserRelMap.put("tenant_id",oldRoleUserRelMap.get("group_id"));
        result = ServiceCommon.compareMapValue(oldRoleUserRelMap,newRoleUserRelMap,
                Lists.newArrayList("tenant_id","role_id","user_id"),
                "角色人员关系ID:"+oldRoleUserRelMap.get("id"),
                logger);
      }

      //重复角色人员关系检查
      List<Map> sameRoleUserRelList = NewUcenterDB.getSqlResult("select * from sys_role_user_rel where id != "+oldRoleUserRelMap.get("id")
              +" and tenant_id = "+oldRoleUserRelMap.get("group_id")
              +" and user_id = "+oldRoleUserRelMap.get("user_id")
              +" and role_id = "+oldRoleUserRelMap.get("role_id"));
      if (!CollectionUtils.isEmpty(sameRoleUserRelList)){
        logger.error("老运营角色人员关系ID:"+oldRoleUserRelMap.get("id")+" 新运营存在重复角色人员关系记录个数："+sameRoleUserRelList.size());
        result = false;
      }
    }
    return result;
  }

}
